package com.cqie.tes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqie.tes.comm.R;
import com.cqie.tes.comm.qo.BaseQo;
import com.cqie.tes.comm.service.impl.BaseServiceImpl;
import com.cqie.tes.domain.RoleFunc;
import com.cqie.tes.domain.RoleMenu;
import com.cqie.tes.domain.SysFunc;
import com.cqie.tes.domain.SysMenu;
import com.cqie.tes.domain.qo.SysMenuQo;
import com.cqie.tes.domain.vo.SysMenuFuncVo;
import com.cqie.tes.domain.vo.SysMenuTreeVo;
import com.cqie.tes.domain.vo.SysMenuVo;
import com.cqie.tes.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cqie.tes.service.SysMenuService;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private RoleFuncMapper roleFuncMapper;
    @Autowired
    private SysFuncMapper sysFuncMapper;
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;


    @Override
    public R getList(BaseQo query) {
        // 1
        SysMenuQo sysMenuQo = (SysMenuQo) query;
        // 构建查询条件
        List<SysMenuVo> sysMenuVos = new ArrayList<>();
        // 根据角色ID查询授权的菜单ID
//        QueryWrapper<RoleMenu> mqw = new QueryWrapper<>();
//        mqw.eq("roleId", id);
//        List<RoleMenu> roleMenus = roleMenuMapper.selectList(mqw);
//        List<Long> menuIds = new ArrayList<>();
//        roleMenus.forEach(roleMenu -> {
//            menuIds.add(roleMenu.getMenuId());
//        });


        // 父级菜单列表
        QueryWrapper<SysMenu> fqw = new QueryWrapper<>();
        fqw.eq("mark", 1);
        fqw.orderByAsc("sortCode");
        List<SysMenu> sysFMenuList = sysMenuMapper.selectList(fqw);
        sysFMenuList.forEach(sysFMenu -> {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setId("menuId_"+sysFMenu.getId());
            sysMenuVo.setParentId("menuId_"+sysFMenu.getParentId());
            sysMenuVo.setName(sysFMenu.getName());
            sysMenuVo.setMenuIcon(sysFMenu.getImgUrl());
            sysMenuVo.setMenu(true);
            sysMenuVo.setOrderNumber(sysFMenu.getSortCode());
//            sysMenuVo.setMenuUrl(sysFMenu.getAppUrl()==null?null:sysFMenu.getAppUrl());
            sysMenuVo.setMenuUrl(sysFMenu.getAppUrl());

            sysMenuVos.add(sysMenuVo);
        });

        QueryWrapper<SysFunc> funcqw = new QueryWrapper<>();
        funcqw.eq("mark", 1);
        funcqw.orderByAsc("sortCode");
        List<SysFunc> sysFuncList = sysFuncMapper.selectList(funcqw);
        sysFuncList.forEach(sysFunc -> {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setId("funcId_"+sysFunc.getId());
            sysMenuVo.setParentId("menuId_"+sysFunc.getParentId());
            sysMenuVo.setName(sysFunc.getName());
            sysMenuVo.setMenuIcon(sysFunc.getImgUrl());
            sysMenuVo.setMenu(false);
            sysMenuVo.setOrderNumber(sysFunc.getSortCode());
//            sysMenuVo.setMenuUrl(sysFMenu.getAppUrl()==null?null:sysFMenu.getAppUrl());
            sysMenuVo.setMenuUrl(sysFunc.getAppUrl());

            sysMenuVos.add(sysMenuVo);
        });



        return R.ok().message("查询成功").data("rows",sysMenuVos).data("total",sysMenuVos.size());
    }

    /**
     * 菜单结构树
     * */
    @Override
    public R treeList() {

        // 结果列表
        List<SysMenuTreeVo> sysMenuTreeVos  =new ArrayList<>();

        // 一级菜单
        QueryWrapper<SysMenu> fqw = new QueryWrapper<>();
        fqw.eq("mark", 1);
        fqw.eq("parentId", 0);
        fqw.orderByAsc("sortCode");
        List<SysMenu> sysFMenuList = sysMenuMapper.selectList(fqw);
        sysFMenuList.forEach(sysFMenu -> {
            SysMenuTreeVo sysFMenuTreeVo = new SysMenuTreeVo();
            sysFMenuTreeVo.setId(sysFMenu.getId());
            sysFMenuTreeVo.setName(sysFMenu.getName());
            // 先加是为了顺序
            sysMenuTreeVos.add(sysFMenuTreeVo);
            // 二级菜单
            QueryWrapper<SysMenu> cqw = new QueryWrapper<>();
            cqw.eq("mark", 1);
            cqw.eq("parentId", sysFMenu.getId());
            cqw.orderByAsc("sortCode");
            List<SysMenu> sysCMenuList = sysMenuMapper.selectList(cqw);
            sysCMenuList.forEach(sysCMenu -> {
                SysMenuTreeVo sysCMenuTreeVo = new SysMenuTreeVo();
                sysCMenuTreeVo.setId(sysCMenu.getId());
                sysCMenuTreeVo.setName("∟" + sysCMenu.getName());
                // 先加是为了顺序
                sysMenuTreeVos.add(sysCMenuTreeVo);
            });
        });
        return R.ok().message("查询成功").data("tree", sysMenuTreeVos);
    }


    @Override
    public R deleteById(Integer id) {
        if (id == null || id == 0) {
            return R.error().message("记录ID不能为空");
        }
        SysMenu entity = this.getById(id);
        if (entity == null) {
            return R.error().message("记录不存在");
        }
        // 删除的操作的id列表
        List<Long> fids = new ArrayList<>();
        // 删除操作
        QueryWrapper<SysFunc> sfqw = new QueryWrapper<>();
        sfqw.eq("menuId",entity.getId());
        List<SysFunc> sysFuncList = sysFuncMapper.selectList(sfqw);
        sysFuncList.forEach(item->{
            fids.add(item.getId());
        });
        sysFuncMapper.delete(sfqw);
        // 删除rolefunc
        if (!fids.isEmpty()){
            roleFuncMapper.deleteBatchIds(fids);
        }
        // 删除子级菜单
        QueryWrapper<SysMenu> smqw = new QueryWrapper<>();
        smqw.eq("parentId",entity.getId());
        sysMenuMapper.delete(smqw);

        // 删除菜单
        return super.delete(entity);
    }
}
